package com.guava;

import com.google.common.util.concurrent.RateLimiter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.TimeUnit;

/**
 * Created by fangjicai on 2020/10/28.
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    /**
     * test接口,1秒生成1个令牌,也就是1秒钟允许一个人访问
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new RateLimiterInterceptor(RateLimiter.create(1,1, TimeUnit.SECONDS)))
                .addPathPatterns("/test");

    }
}
